﻿/* Copyright (c) 2010 Lexalytics Inc.

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace Lexalytics4Excel
{
    /// <summary>Collection class for profiles used with Lexalytics Zephyr web service.</summary>
    public class Profiles : Dictionary<string, Profile>
    {
        /// <summary>String list of profile names</summary>
        public List<string> ProfileNames { get { return new List<string>(this.Keys); } }

        /// <summary>Default constructor</summary>
        public Profiles()
        {
        }
        /// <summary>
        /// Constructor with single Profile object to initialize collection.
        /// </summary>
        /// <param name="ProfileName">Descriptive name for the profile</param>
        /// <param name="ProfileContents">A Profile object</param>
        public Profiles(string ProfileName, Profile ProfileContents)
        {
            this.Add(ProfileName, ProfileContents);
        }
        /// <summary>
        /// Constructor with formatted StringCollection to initialize collection class. 
        /// </summary>
        /// <param name="ProfileSet">A StringCollection formatted with the information needed for instantiating Profile objects for the collection.</param>
        public Profiles(StringCollection ProfileSet)
        {
            foreach (string strProfile in ProfileSet)
            {
                string[] strProfileBits = strProfile.Split(new char[] { '|' }, 5, StringSplitOptions.None);
                if (strProfileBits.GetLength(0) == 4)
                {
                    Profile myProfile = new Profile()
                    {
                        ProfileName = strProfileBits[0],
                        ServiceURL = strProfileBits[1],
                        PublicKey = strProfileBits[2],
                        PrivateKey = strProfileBits[3],
                        LocalProfile = false
                    };
                    if (myProfile.IsValid())
                        this.Add(myProfile.ProfileName, myProfile);
                }
                else
                {
                    if (strProfileBits[4].Equals("1"))
                    {
                        Profile myProfile = new Profile()
                        {
                            ProfileName = strProfileBits[0],
                            LicensePath = strProfileBits[1],
                            DataDirectory = strProfileBits[2],
                            UserDirectory = strProfileBits[3],
                            LocalProfile = true
                        };
                        if (myProfile.IsValid())
                            this.Add(myProfile.ProfileName, myProfile);
                    }
                    else
                    {
                        Profile myProfile = new Profile()
                        {
                            ProfileName = strProfileBits[0],
                            ServiceURL = strProfileBits[1],
                            PublicKey = strProfileBits[2],
                            PrivateKey = strProfileBits[3],
                            LocalProfile = false
                        };
                        if (myProfile.IsValid())
                            this.Add(myProfile.ProfileName, myProfile);
                    }
                }

            }
        }

        /// <summary>
        /// Method to extract contents of collection in a formatted String collection for settings storage.
        /// </summary>
        /// <returns>Formatted StringCollection object which can be persisted in application settings.</returns>
        public StringCollection GetProfileCollection()
        {
            StringCollection allProfiles = new StringCollection();
            foreach (string aProfileName in this.Keys)
            {
                Profile aProfile = this[aProfileName];
                if (aProfile.LocalProfile)
                    allProfiles.Add(String.Format("{0}|{1}|{2}|{3}|1",aProfileName, aProfile.LicensePath, aProfile.DataDirectory, aProfile.UserDirectory));
                else
                    allProfiles.Add(String.Format("{0}|{1}|{2}|{3}|0", aProfileName, aProfile.ServiceURL, aProfile.PublicKey, aProfile.PrivateKey));
            }
            return allProfiles;
        }
    }

    /// <summary>
    /// Class to encapsulate the information needed to connect to the Lexalytics Zephyr web service
    /// </summary>
    public class Profile
    {
        /// <summary>Descriptive name for the profile</summary>
        public string ProfileName = "";
        /// <summary>Flag indicating if profile is local or online </summary>
        public bool LocalProfile = true;
        /// <summary>Base web service URL for the profile</summary>
        public string ServiceURL = "";
        /// <summary>Public key to be sent with each web service request</summary>
        public string PublicKey = "";
        /// <summary>Private key used for encoding requests for server-side validation</summary>
        public string PrivateKey = "";

        /// <summary>Path to a local license file</summary>
        public string LicensePath = "";
        /// <summary>Path to data directory</summary>
        public string DataDirectory = "";
        /// <summary>Path to user directory</summary>
        public string UserDirectory = "";

        /// <summary>Default constructor</summary>
        public Profile() { }

        /// <summary>
        /// Confirms mandatory information exists for Profile
        /// </summary>
        /// <returns>Boolean value indicating if Profile contains needed information</returns>
        public bool IsValid()
        {
            bool bValid = false;

            if (LocalProfile)
            {
                // Check for existence of license file
                // Check for existence of data directory
                // Check for existence of user directory
                bValid = true;
            }
            else
            {
                bValid = (ProfileName.Length > 0 && ServiceURL.Length > 0 && PublicKey.Length > 0 && PrivateKey.Length > 0);
            }
            return bValid;
        }
    }
}
